home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Drivers / Fd / clri / clri.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-31  |  7.2 KB  |  249 lines

  1. /*
  2.  * Copyright (c) 1990, 1993
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Rich $alz of BBN Inc.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  */
  36.  
  37. /*
  38.  *    Modified by John L. Chmielewski on Sat Dec 30, 1995
  39.  *    Hacked code to work under NEXTSTEP/FIP 3.3
  40.  *    Modified by Mark Salyzyn on Sun Dec 31, 1995 to add some UI.
  41.  */
  42.  
  43. #ifndef lint
  44. static char copyright[] =
  45. "@(#) Copyright (c) 1990, 1993\n\
  46.     The Regents of the University of California.  All rights reserved.\n";
  47. #endif /* not lint */
  48.  
  49. #ifndef lint
  50. static char sccsid[] = "@(#)clri.c    8.2 (Berkeley) 9/23/93";
  51. #endif /* not lint */
  52.  
  53. #include <sys/param.h>
  54. #include <sys/time.h>
  55.  
  56. #ifdef NeXT
  57. #include <sys/vnode.h>
  58. #include <ufs/inode.h>
  59. #include <ufs/fs.h>
  60. #include <dev/disk.h>
  61. /* DEV_BSIZE should not be hardcoded */
  62. #define    DEV_BSIZE    fsize    /*    512 for some >1G drives
  63.                     1024 othersize    */
  64. #define ino_to_fsba itod
  65. #define ino_to_fsbo itoo
  66. #include <architecture/byte_order.h>
  67. /* must redefine header file macros for LITTLE_ENDIAN, BIG_ENDIAN also works */
  68. #undef fsbtodb
  69. #define fsbtodb(fs, b)    ((b) << NXSwapBigLongToHost((fs)->fs_fsbtodb))
  70. #undef INOPB
  71. #define    INOPB(fs)    (NXSwapBigLongToHost((fs)->fs_inopb))
  72. #undef itog
  73. #define    itog(fs, x)    ((x) / NXSwapBigLongToHost((fs)->fs_ipg))
  74. #undef blkstofrags
  75. #define blkstofrags(fs, blks)    /* calculates (blks * fs->fs_frag) */ \
  76.     ((blks) << NXSwapBigLongToHost((fs)->fs_fragshift))
  77. #undef cgbase
  78. #define    cgbase(fs, c)    ((daddr_t)(NXSwapBigLongToHost((fs)->fs_fpg) * (c)))
  79. #undef cgstart
  80. #define cgstart(fs, c) \
  81.     (cgbase(fs, c) + NXSwapBigLongToHost((fs)->fs_cgoffset) * ((c) \
  82.     & ~(NXSwapBigLongToHost((fs)->fs_cgmask))))
  83. #undef cgimin
  84. #define    cgimin(fs, c)    (cgstart(fs, c) + NXSwapBigLongToHost((fs)->fs_iblkno))
  85. #undef itod
  86. #define    itod(fs, x) \
  87.     ((daddr_t)(cgimin(fs, itog(fs, x)) + \
  88.     (blkstofrags((fs), (((x) % NXSwapBigLongToHost((fs)->fs_ipg)) / \
  89.     INOPB(fs))))))
  90. #else
  91. #include <ufs/ufs/dinode.h>
  92. #include <ufs/ffs/fs.h>
  93.  
  94. #include <err.h>
  95. #endif
  96. #include <errno.h>
  97. #include <fcntl.h>
  98. #include <stdlib.h>
  99. #include <string.h>
  100. #include <stdio.h>
  101. #include <unistd.h>
  102.  
  103. int
  104. main(argc, argv)
  105.     int argc;
  106.     char *argv[];
  107. {
  108.     register struct fs *sbp;
  109.     register struct dinode *ip;
  110.     register int fd;
  111.     struct dinode ibuf[MAXBSIZE / sizeof (struct dinode)];
  112.     long generation, bsize, fsize = 1024;
  113.     off_t offset;
  114.     int inonum, i, c;
  115.     char *fs, sblock[SBSIZE];
  116.     char nowrite = 0, noask = 0 ;
  117.  
  118.     while (fs = *++argv) {
  119.         if (--argc == 0)
  120.             break ;
  121.         if (strcmp (fs, "-w") == 0)
  122.             nowrite = 1 ;
  123.         else if (strcmp (fs, "-y") == 0)
  124.             noask = 1 ;
  125.         else if (strcmp (fs, "-b") == 0)
  126.             fsize = 512 ;
  127.         else    break ;
  128.     }
  129.     if ((argc <= 0) || (fs == NULL)) {
  130. usage:        (void)fprintf(stderr, "usage: clri [-w] [-y] [-b] filesystem inode ...\n");
  131.         exit(1);
  132.     }
  133.  
  134.     /* get the superblock. */
  135.     if ((fd = open(fs, O_RDWR, 0)) < 0)
  136.         err(1, "%s", fs);
  137. #ifndef NeXT
  138.     if (lseek(fd, (off_t)(SBLOCK * DEV_BSIZE), SEEK_SET) < 0)
  139. #else
  140.     if (lseek(fd, (off_t)(SBLOCK), SEEK_SET) < 0)
  141. #endif
  142.         err(1, "%s", fs);
  143.     if (read(fd, sblock, sizeof(sblock)) != sizeof(sblock)) {
  144.         (void)fprintf(stderr,
  145.             "clri: %s: can't read the superblock.\n", fs);
  146.         exit(1);
  147.     }
  148.  
  149.     sbp = (struct fs *)sblock;
  150. #ifndef NeXT
  151.     if (sbp->fs_magic != FS_MAGIC) {
  152. #else
  153.     if (NXSwapBigLongToHost(sbp->fs_magic) != FS_MAGIC) {
  154. #endif
  155.         (void)fprintf(stderr,
  156.             "clri: %s: superblock magic number 0x%x, not 0x%x.\n",
  157.             fs, sbp->fs_magic, FS_MAGIC);
  158.         exit(1);
  159.     }
  160. #ifndef NeXT
  161.     bsize = sbp->fs_bsize;
  162. #else
  163.     bsize = NXSwapBigLongToHost(sbp->fs_bsize);
  164. #endif
  165.  
  166.     /* remaining arguments are inode numbers. */
  167.     while (*++argv) {
  168.         /* get the inode number. */
  169.         if ((inonum = atoi(*argv)) <= 0) {
  170.             (void)fprintf(stderr,
  171.                 "clri: %s is not a valid inode number.\n", *argv);
  172.             exit(1);
  173.         }
  174.         (void)printf("%sinode: %d ", nowrite?"":"clearing ", inonum);
  175.  
  176.         /* read in the appropriate block. */
  177.         offset = ino_to_fsba(sbp, inonum);    /* inode to fs blk */
  178.         offset = fsbtodb(sbp, offset);        /* fs blk disk blk */
  179.         offset *= DEV_BSIZE;            /* disk blk to bytes */
  180.  
  181.         /* seek and read the block */
  182.         if (lseek(fd, offset, SEEK_SET) < 0)
  183.             err(1, "%s", fs);
  184.         if (read(fd, ibuf, bsize) != bsize)
  185.             err(1, "%s", fs);
  186.  
  187.         /* get the inode within the block. */
  188.         ip = &ibuf[ino_to_fsbo(sbp, inonum)];
  189.  
  190.         /* clear the inode, and bump the generation count. */
  191. #ifndef NeXT
  192.         (void)printf("mode: %o nlink: %d uid: %d gid: %d size: %d\n",
  193.             ip->di_mode, ip->di_nlink, ip->di_uid,
  194.             ip->di_gid, ip->di_size);
  195.         generation = ip->di_gen + 1;
  196. #else
  197.         (void)printf("mode: %o nlink: %d uid: %d gid: %d size: %d\n",
  198.             NXSwapBigShortToHost(ip->di_mode),
  199.             NXSwapBigShortToHost(ip->di_nlink),
  200.             NXSwapBigShortToHost(ip->di_uid),
  201.             NXSwapBigShortToHost(ip->di_gid),
  202.             NXSwapBigLongToHost(ip->di_ic.ic_size.val[1]));
  203.         generation = NXSwapBigLongToHost(ip->di_gen) + 1;
  204. #endif
  205.         memset(ip, 0, sizeof(*ip));
  206. #ifndef NeXT
  207.         ip->di_gen = generation;
  208. #else
  209.         ip->di_gen = NXSwapHostLongToBig(generation);
  210. #endif
  211.  
  212.         if (nowrite == 0) {
  213.             if (noask == 0) {
  214.                 (void)fprintf (stderr, "Clear? ") ;
  215. again:                c = getchar () ;
  216.                 while (((i = getchar ()) != EOF)
  217.                  && (i != '\n') && (i != '\r')) ;
  218.                 switch (c) {
  219.                 case 'Y': case 'y':
  220.                     break ;
  221.                 case 'N': case 'n':
  222.                     continue ;
  223.                 default:
  224.                     (void)fprintf (stderr, "Yes or No? ") ;
  225.                     goto again;
  226.                 }
  227.             }
  228.             /* backup and write the block */
  229.             if (lseek(fd, (off_t)-bsize, SEEK_CUR) < 0)
  230.                 err(1, "%s", fs);
  231.             if (write(fd, ibuf, bsize) != bsize)
  232.                 err(1, "%s", fs);
  233.             (void)fsync(fd);
  234.         }
  235.     }
  236.     (void)close(fd);
  237.     exit(0);
  238. }
  239.  
  240. #ifdef NeXT
  241. err(a, b, fs)
  242.     int a;
  243.     char *b;
  244.     char *fs;
  245. {
  246.     perror(fs);
  247. }
  248. #endif
  249.